<?php

class __micro_ajax extends Module {
	
	public function __construct() {
		parent::__construct('micro');
	}
	
	public function ajax_add_post() {
		$typesController = TypesController::getInstance();
		
		$oTypeMicro = $typesController->getTypeByName('micro');
		
		$oMicro = new Object($oTypeMicro);
		$oMicro->setValue('pubdate', time());
		$oMicro->setValue('owner_id', ExplaySession::getUserId());
		$oMicro->setValues($this->getRequestData('new', $oTypeMicro));
		
		preg_match_all("/\#([a-zа-я0-9-_]*)/iu", $oMicro->getValue('text'), $tags);
		$oMicro->setValue('tags',$tags[1]);
		
		$aErrors = $this->getValidateErrors($oMicro);
		
		if (count($aErrors)) {
			$this->flush((string) new Array2XML('data', array('result' => 0)), 'text/xml');
		}
		
		$oMicro->createNew();
		
		$this->addMicroLog($oMicro);
		
		$this->flush((string) new Array2XML('data', array('result' => 1)), 'text/xml');
	}
	
	private function addMicroLog (Object $oMicro) {
		if (Regedit::getInstance()->get ('//settings/site/use_logs') == 1) {
			$oLog = new Object (TypesController::getInstance()->getTypeByName('log_micro'));
			$oLog->setValues (array (
				'date' => time (),
				'micro_id' => $oMicro->getId (),
				'user_id' => ExplaySession::getUserId ()
			));
			$oLog->createNew ();
		}
	}
}